package com.sqx.modules.sharingEncryption.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.sharingEncryption.dao.SharingEncryptionDao;
import com.sqx.modules.sharingEncryption.entity.SharingEncryption;
import com.sqx.modules.sharingEncryption.service.SharingEncryptionService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * @author Admin
 * @description 针对表【sharing_encryption(分享加密表)】的数据库操作Service实现
 * @createDate 2024-12-12 15:56:32
 */
@Service
public class SharingEncryptionServiceImpl extends ServiceImpl<SharingEncryptionDao, SharingEncryption>
        implements SharingEncryptionService {


    @Override
    public Result addSharingEncryption(SharingEncryption sharingEncryption) {
        // 定义链接的固定前缀
//        String fixedPrefix = "https://www.xianyuSharing.com/link/";
        // 生成随机的UUID并去掉所有"-"，以确保URL中没有非法字符
        String randomSuffix = UUID.randomUUID().toString().replaceAll("-", "");
        // 组合固定前缀与随机后缀形成最终链接
//        String encryptLink = fixedPrefix + randomSuffix;
        sharingEncryption.setEncryptLink(randomSuffix);
        sharingEncryption.setCreateTime(new Date());
        int result = baseMapper.insert(sharingEncryption);
        if (result <= 0) {
            return Result.error("分享失败");
        }
        return Result.success().put("data", sharingEncryption);
    }

    @Override
    public Result selectByEncryptLink(String encryptLink) {
        SharingEncryption sharingEncryption = baseMapper.selectOne(new QueryWrapper<SharingEncryption>()
                .like("encrypt_link", '%' + encryptLink + '%'));
        if (sharingEncryption == null) {
            return Result.error("链接失效");
        }
        return Result.success().put("data", sharingEncryption);
    }
}




